package butines.game2.entity;

import butines.common.math.Vector2D;
import butines.core.GameImage;
import butines.game2.Drawer;
import butines.game2.Game;
import butines.game2.Team;
import butines.game2.messaging.Message;

public abstract class Entity {

    
    public static enum Status {
        ALIVE, DEAD
    }
    
    private static int nextValidID = 1;

    protected final Drawer drawer = Drawer.getInstance();
    
    private final int id;
    
    protected Game game;
    protected Team team;
    protected Status status;
    protected boolean selected;
    protected boolean selectable;
    protected boolean canAttack;
    
    protected int life;
    protected int hitPoints;
    
    protected final Vector2D position;
    
    protected GameImage image;
    
    public Entity() {
        id = getNextValidID();
        position = new Vector2D();
    }
    
    private int getNextValidID(){
        return nextValidID++;
    }
    
    public boolean handleMessage(Message message) {
        return false;
    }
    
    
    public void update(float dt) {
        
    }
    
    public void renderImage(float sx, float sy) {
        drawer.image(image, sx, sy);
    }
    
    public void renderSelection(float sx, float sy) {

    }
    
    public void renderLifeBar(float sx, float sy) {

    }
    
    public void renderDebug(float sx, float sy) {
    	
    }
    
    public boolean isAlive() {
        return status == Status.ALIVE;
    }
    
    public boolean isDead() {
        return status == Status.DEAD;
    }
    
    public boolean isUnit() {
        return this instanceof Unit;
    }
    
    public boolean isBuilding() {
        return this instanceof Building;
    }
    
    public Building asBuilding() {
        return isBuilding() ? (Building) this : null;
    }
    
    public Unit asUnit() {
        return isUnit() ? (Unit) this : null;
    }
    
    
    
    /** GETTERS AND SETTERS *******************************************/
    public int getId() {
        return id;
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public boolean isSelectable() {
        return selectable;
    }

    public void setSelectable(boolean selectable) {
        this.selectable = selectable;
    }

    public boolean isCanAttack() {
        return canAttack;
    }

    public void setCanAttack(boolean canAttack) {
        this.canAttack = canAttack;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public int getHitPoints() {
        return hitPoints;
    }

    public void setHitPoints(int hitPoints) {
        this.hitPoints = hitPoints;
    }

    public Vector2D getPosition() {
        return position;
    }
    
    public void setPosition(int x, int y) {
        position.x = x;
        position.y = y;
    }
    
    public float getX() {
        return position.x;
    }
    
    public float getY() {
        return position.y;
    }
    
    public int getGridX() {
        return (int) (position.x / game.getCellSize());
    }
    
    public int getGridY() {
        return (int) (position.y / game.getCellSize());
    }

    public GameImage getImage() {
        return image;
    }

    public void setImage(GameImage image) {
        this.image = image;
    }
    
}
